package mobi.greenplate.theweb.user;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import org.deltaset.faces.FacesUtils;

/**
 *
 * @author Dimitar Makariev
 */
public class PreferencesPhaseListener implements PhaseListener {

    public void afterPhase(PhaseEvent event) {
        final PreferencesBean preferencesBean = FacesUtils.getManagedBean("preferencesBean", PreferencesBean.class);
        final String lang = FacesUtils.getParameter("lang");
        if (!"".equals(lang)) {
            preferencesBean.setCurrentLocaleLanguage(lang);
            preferencesBean.saveUserContext();
        }
        FacesContext.getCurrentInstance().getViewRoot().setLocale(preferencesBean.getCurrentLocale());
    }

    public void beforePhase(PhaseEvent event) {
        //do nothing
    }

    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }
}
